/*
 * @Descripttion: GOGOGO
 * @version: 1.0.0
 * @Author: XJM
 * @Date: 2022-08-11 19:24:53
 * @LastEditors: XJM
 * @LastEditTime: 2022-08-22 16:32:34
 */
import { createRouter, createWebHashHistory } from 'vue-router'
import store from '@/store'

const routes = [
  // 登录页面
  {
    path: '/login',
    name: 'login',
    component: () => import('../views/Pages/login.vue')
  },
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/layout',
    name: 'layout',
    component: () => import('../views/LayOut/LayOut.vue'),
    children: [
      {
        path: '/goods',
        name: 'goods',
        component: () => import('../views/Pages/goodsList')
      },
      {
        path: '/user',
        name: 'user',
        component: () => import('../views/Pages/usersList')
      },
      {
        path: '/roles',
        name: 'roles',
        component: () => import('../views/Pages/rolesList.vue')
      },
      {
        path: '/index',
        name: 'index',
        component: () => import('../views/Pages/index.vue')
      },
      {
        path: '/statistics',
        name: 'statistics',
        component: () => import('../views/Pages/dataStatistics.vue')
      }
    ]
  }
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})

//路由守卫
router.beforeEach((to, from, next) => {
  /* 
  to:从那个页面
  from：到哪个页面
  next：只有执行next()，页面才会进行跳转
  */
  //  判断用户是否登录
  // console.log('store', store.state.userinfo);
  if (!(store.state.userinfo.userInfo.username)) {
    // 未登录,跳转到login
    if (to.path === '/login') {
      next()
      return
    }
    next('/login')
  } else {
    next()
  }



})

export default router
